{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Variable & Automatic Gradient Calculation\n",
    "- Tensor vs Variable\n",
    "- Graph and Gradient"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.Import Required Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Tensor vs Variable\n",
    "### 1) Declaration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "-12170.8672      0.0000 -12170.8672      0.0000\n",
       "     0.0000      0.0000      0.0000      0.0000\n",
       "-12170.9453      0.0000 -12170.9453      0.0000\n",
       "[torch.FloatTensor of size 3x4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_tensor = torch.Tensor(3,4)\n",
    "x_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable containing:\n",
       "-12170.8672      0.0000 -12170.8672      0.0000\n",
       "     0.0000      0.0000      0.0000      0.0000\n",
       "-12170.9453      0.0000 -12170.9453      0.0000\n",
       "[torch.FloatTensor of size 3x4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_variable = Variable(x_tensor)\n",
    "x_variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) Variables of a Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "-12170.8672      0.0000 -12170.8672      0.0000\n",
       "     0.0000      0.0000      0.0000      0.0000\n",
       "-12170.9453      0.0000 -12170.9453      0.0000\n",
       "[torch.FloatTensor of size 3x4]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .data -> wrapped tensor \n",
    "x_variable.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# .grad -> gradient of the variable\n",
    "print(x_variable.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .requires_grad -> whether variable requres gradient\n",
    "print(x_variable.requires_grad)\n",
    "\n",
    "x_variable = Variable(x_tensor,requires_grad=True)\n",
    "x_variable.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(None, False, True)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .volatile -> inference mode with minimal memory usage\n",
    "# single volatile variable makes whole graph not requiring gradient\n",
    "\n",
    "x_variable = Variable(x_tensor,volatile=True)\n",
    "x_variable.grad, x_variable.requires_grad, x_variable.volatile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Graph & Variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True, True)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create graph\n",
    "\n",
    "x = Variable(torch.FloatTensor(3,4),requires_grad=True)\n",
    "y = x**2 + 4*x\n",
    "z = 2*y +3\n",
    "\n",
    "x.requires_grad,y.requires_grad,z.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable containing:\n",
      " 5.9243e+08  3.6696e-40 -9.7367e+04  3.6696e-40\n",
      " 1.2044e-37  0.0000e+00  0.0000e+00  0.0000e+00\n",
      " 1.3372e+26  3.6696e-40 -1.8382e-09  3.6696e-40\n",
      "[torch.FloatTensor of size 3x4]\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(None, None)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .backward(gradient,retain_graph,create_graph,retain_variables)\n",
    "# compute gradient of current variable w.r.t. graph leaves\n",
    "\n",
    "gradient = torch.FloatTensor(3,4)\n",
    "z.backward(gradient)\n",
    "\n",
    "print(x.grad)\n",
    "y.grad,z.grad"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
